version: "3.9"

x-assets-volume: &assets-volume ./media/assets:${PROJECT_PATH:-/usr/src/app}/media/assets
x-reviews-volume: &reviews-volume ./media/reviews:${PROJECT_PATH:-/usr/src/app}/media/reviews
x-invoices-volume: &invoices-volume ./media/invoices:${PROJECT_PATH:-/usr/src/app}/media/invoices
x-translations-volume: &translations-volume ./poker_bot/tg_bot/locales:${PROJECT_PATH:-/usr/src/app}/poker_bot/tg_bot/locales

services:
  tg_bot:
    container_name: poker_bot.tg_bot
    image: "${BOT_IMAGE_NAME:-poker_bot.poker_bot}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - *assets-volume
      - *reviews-volume
      - *invoices-volume
      - *translations-volume
    networks:
      - poker_bot.postgres.network
      - poker_bot.redis.network
    environment:
      - TZ=Europe/Moscow
    env_file:
      - ".env"
    command: [ "python", "-m", "poker_bot.tg_bot" ]
  admin_panel:
    container_name: poker_bot.admin_panel
    image: "${BOT_IMAGE_NAME:-poker_bot.admin_panel}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "5000:5000"
    volumes:
      - *assets-volume
      - *reviews-volume
      - *invoices-volume
      - *translations-volume
    networks:
      - poker_bot.postgres.network
    environment:
      - TZ=Europe/Moscow
    env_file:
      - ".env"
    command: [ "gunicorn", "poker_bot.admin_panel.wsgi:app", "--workers", "2", "--threads", "2", "-b", "0.0.0.0:5000" ]
  worker:
    container_name: poker_bot.worker
    image: "${BOT_IMAGE_NAME:-poker_bot.worker}"
    build:
      context: .
    restart: unless-stopped
    stop_signal: SIGINT
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - poker_bot.postgres.network
      - poker_bot.redis.network
    environment:
      - TZ=Europe/Moscow
    env_file:
      - ".env"
    command: [ "arq", "poker_bot.tg_bot.worker.main.WorkerSettings" ]
  redis:
    container_name: poker_bot.redis
    image: redis:7.0.10-alpine
    command:
      sh -c '
      redis-server
      --appendonly yes
      --bind 0.0.0.0'
    volumes:
      - poker_bot.redis.data:/data:rw
    ports:
      - "${REDIS__PORT:-6379}:6379"
    networks:
      - poker_bot.redis.network
    healthcheck:
      test: [ "CMD-SHELL", "redis-cli --no-auth-warning ping | grep '^PONG$' || exit 1" ]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 10s
    environment:
      - TZ=Europe/Moscow
    env_file:
      - ".env"
  postgres_migration:
    image: "${BOT_IMAGE_NAME:-poker_bot.poker_bot}"
    container_name: poker_bot.postgres_migration
    build:
      context: .
    restart: on-failure
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - poker_bot.postgres.network
    volumes:
      - .:${PROJECT_PATH:-/usr/src/app}
      - ./alembic.ini:${PROJECT_PATH:-/usr/src/app}/alembic.ini:ro
    env_file:
      - ".env"
    command: [ "python", "-m", "alembic", "upgrade", "head" ]
  postgres:
    container_name: poker_bot.postgres
    image: "postgres:15-alpine"
    hostname: poker_bot.postgres
    restart: unless-stopped
    expose:
      - "5432"
    ports:
      - "${DB__PORT:-5432}:5432"
    networks:
      - poker_bot.postgres.network
    environment:
      POSTGRES_PASSWORD: $DB__PASSWORD
      POSTGRES_USER: $DB__USER
      POSTGRES_DB: $DB__NAME
      TZ: Europe/Moscow
    volumes:
      - poker_bot.postgres.data:/var/lib/postgresql/users:rw
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 10s
  pgadmin:
    container_name: poker_bot.pgadmin
    image: dpage/pgadmin4:latest
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin@example.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_LISTEN_PORT: ${PGADMIN_LISTEN_PORT:-5050}
    volumes:
      - poker_bot.pgadmin.data:/var/lib/pgadmin
    ports:
      - "${PGADMIN_LISTEN_PORT:-5050}:5050"
    expose:
      - "${PGADMIN_LISTEN_PORT:-5050}"
    networks:
      - poker_bot.postgres.network
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - ".env"

volumes:
  poker_bot.postgres.data: {}
  poker_bot.redis.data: {}
  poker_bot.pgadmin.data: {}

networks:
  poker_bot.postgres.network: {}
  poker_bot.redis.network: {}
